Skip to content

Conversation

@juan518munoz
Copy link
Contributor

@juan518munoz juan518munoz commented Jul 18, 2025

The current call methods calls the GenServer and can hypothetically get stuck there, this new method prevents this from happening.

As a follow up, it may be suitable to rewrite call so it also works with a pre-defined, relaxed, timeout:

    const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);

    pub async fn call(&mut self, message: G::CallMsg) -> Result<G::OutMsg, GenServerError> {
        self.call_with_timeout(message, Self::DEFAULT_TIMEOUT).await
    }

    pub async fn call_with_timeout(
        &mut self,
        message: G::CallMsg,
        duration: Duration,
    ) -> Result<G::OutMsg, GenServerError> {
        let (oneshot_tx, oneshot_rx) = oneshot::channel::<Result<G::OutMsg, GenServerError>>();
        self.tx.send(GenServerInMsg::Call {
            sender: oneshot_tx,
            message,
        })?;

        match timeout(duration, oneshot_rx).await {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(GenServerError::Server),
            Err(_) => Err(GenServerError::CallTimeout),
        }
    }

Closes #6

Copy link
Collaborator

@ElFantasma ElFantasma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
:shipit:

@juan518munoz juan518munoz merged commit eb54b1f into main Jul 18, 2025
3 checks passed
@juan518munoz juan518munoz deleted the add_call_timeout branch July 18, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Offer a timeout method

4 participants